<template>
  <el-dialog
    title="更新数据"
    width="30%"
    :append-to-body="true"
    :visible.sync="showTag"
  >
    <span>正在更新{{ form.name }} {{ action }}</span>
  </el-dialog>
</template>

<script>
export default {
  name: "FlinkCacheUpdateDialog",
  data() {
    return {
      form: {},
      action: "",
      showTag: false
    };
  },
  methods: {
    show(id) {
      this.showTag = true;
      if (id) {
        this.doUpdateById(id);
      } else {
        this.doUpdate(1);
      }
    },
    hide() {
      this.showTag = false;
    },
    async doUpdateById(id) {
      this.form = await this.$flinkService.detail(id);
      await this.updateActions();
      this.$emit("ok");
      this.hide();
    },
    async doUpdate(pageNum) {
      const data = await this.$flinkService.list({
        pageSize: 1,
        pageNum
      });
      if (data && data.data && data.data.length) {
        this.form = data.data[0];
        await this.updateActions();
        await this.doUpdate(++pageNum);
      } else {
        this.$emit("ok");
        this.hide();
      }
    },

    async updateActions() {
      await this.updateCateName();
      await this.updateSort();
    },

    async updateSort() {
      this.action = "排序";
      const { form } = this;
      if (!form.sort) {
        await this.$flinkService.update({ id: form.id, sort: form.id });
      }
    },

    async updateCateName() {
      this.action = "分类名称";
      const { form } = this;
      const { name } = await this.$cateService.detail(form.cate_id);
      await this.$flinkService.update({ id: form.id, cate_name: name });
    }
  }
};
</script>

<style scoped></style>
